package com.cykj.controller;

import com.alibaba.fastjson2.JSON;
import com.cykj.dto.ResponseDTO;
import com.cykj.entity.AdminDO;
import com.cykj.service.IAdminService;
import com.cykj.vo.AdminVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    // 访问地址： 127.0.0.1:8080/jd-admin-service/admin/list
    // 根据 AdminVO 模糊查询管理员列表
    @GetMapping("/list")
    public ResponseDTO list(AdminVO adminVO){
        return adminService.list(adminVO);
    }

    // 访问网址： 127.0.0.1:8080/jd-admin-service/admin/login
    @PostMapping("/login")
    public ResponseDTO login(@RequestBody AdminVO adminVO){
        return adminService.login(adminVO);
    }

    // 访问网址： 127.0.0.1:8080/jd-admin-service/admin/info
    @GetMapping("/info")
    public ResponseDTO info(String token){
        // 从redis中获取管理员信息
        Object adminDO = redisTemplate.opsForValue().get(token);
        AdminDO adminInfo = JSON.parseObject(Objects.requireNonNull(adminDO).toString(), AdminDO.class);
        System.err.println("管理员信息:"+ adminInfo);
        return ResponseDTO.success("获取成功",adminInfo);
    }

    // 访问网址： 127.0.0.1:8080/jd-admin-service/admin/logout
    @GetMapping("/logout")
    public ResponseDTO logout(String token){
        // 删除redis中的管理员信息
        redisTemplate.delete(token);
        return ResponseDTO.success("注销成功");
    }

}
